﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System;

namespace Plugins.Windows
{
    public class TreeListViewItem : TreeViewItem
    {
        #region Varibles

        private int _level = -1;
        private bool isChecked = false;
        private int lineNumber = 0;
        private string content = "";
        private Button brakePointButton;

        #endregion

        public event EventHandler<TreeListViewItemEventArgs> BrakePointChanged;

        public TreeListViewItem()
        {
            // Register MouseRightButtopUp event
            this.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(TreeListViewItem_MouseRightButtonUp);
        }

        #region Properties

        /// <summary>
        /// Gets or set IsChecked
        /// </summary>
        public bool IsChecked
        {
            get { return isChecked; }
            set { 
                isChecked = value;
                
                // Set BrakePoint button asociated with current instance
                if (brakePointButton != null)
                    brakePointButton.Opacity = isChecked ? 1 : 0;

                // Raise BrakePointChanged event
                if (BrakePointChanged != null)
                    BrakePointChanged(this, new TreeListViewItemEventArgs(this));
            }
        }

        /// <summary>
        /// Gets or set line number
        /// </summary>
        public int LineNumber
        {
            get { return lineNumber; }
            set { lineNumber = value; }
        }

        /// <summary>
        /// Gets or sets content
        /// </summary>
        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        /// <summary>
        /// Gets or sets BrakePoint button
        /// </summary>
        public Button BrakePointButton
        {
            get { return brakePointButton; }
            set { brakePointButton = value; }
        }

        /// <summary>
        /// Gets item's hierarchy in the tree
        /// </summary>
        public int Level
        {
            get
            {
                if (_level == -1)
                {
                    TreeListViewItem parent = ItemsControl.ItemsControlFromItemContainer(this) as TreeListViewItem;
                    _level = (parent != null) ? parent.Level + 1 : 0;
                }
                return _level;
            }
        }

        #endregion

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is TreeListViewItem;
        }

        /// <summary>
        /// Delegate method for MouseRightButtonUp event. 
        /// Change propery IsChecked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeListViewItem_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            // Change property only for current instance
            if (e.Source.GetHashCode() == this.GetHashCode())
                IsChecked = !isChecked;
        }
    }
}
